<?php

use common\models\Location;
use kartik\select2\Select2;
use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;

/* @var $this yii\web\view */
/* @var $model common\models\Customer */

$this->title = 'Create Customer';
$this->params['breadcrumbs'][] = ['label' => 'Customer', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<h2 class="page-header"><?= $this->title ?></h2>

<?php $form = ActiveForm::begin() ?>

<?= $form->field($model, 'name') ?>

<?= $form->field($model, 'zip_code')->widget(Select2::className(),
    [
        'data' => ArrayHelper::map(Location::find()->all(), 'id', 'zip_code'),
        'options' => ['placeholder' => 'Please choose a location...']
    ])
?>

<?= $form->field($model, 'city') ?>

<?= $form->field($model, 'province') ?>

<button type="submit" class="btn btn-primary">Create</button>

<?php ActiveForm::end() ?>

<?php $this->beginBlock('scripts') ?>
<script>
    $('#customer-zip_code').change(function () {
        var locationId = $(this).val();
        $.getJSON('../location/detail?id=' + locationId, function (data) {
            $('#customer-city').val(data.city);
            $('#customer-province').val(data.province);
        });
    });
</script>
<?php $this->endBlock() ?>
